﻿L34EventSetter = function(item){
	item.addEvents({
		'keyup' : function(event){
			var iKeyCode = event.keyCode;
			if (!(iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123))) {
				var input = item;
				var value = input.value;
				var parent = input.getParent('tbody');
				var _input = parent.getChildren('input[id$=L34Object_hidden]')[0];
				if (value.length > 3){
					var parent_value = parent.getElements('select[id$=L0Object]')[0].value;
					var req = new Request({
						method: 'get',
						url: '/ajax/geography/?obj_name='+value+'&parent_name='+parent_value,
						onSuccess: function(response) {
							suggest.setStyle('display', 'block');
							suggest.getElements('li').each(function(el){el.destroy()});
							var objects = JSON.decode(response);
							for (var i in objects){
								var opt = Element('li', {
									'class': 'suggest_p',
									'html': objects[i].name+'<br>',
								}).store(
									'obj', objects[i]
								).addEvent('click', function(event){
									obj = this.retrieve('obj');
									input.set('value', obj.name);
									_input.set('value', obj.id+';'+obj.l2_id+';'+obj.l1_id);
									suggest.setStyle('display', 'none');
								}).inject(suggest, 'top');
								if(objects[i].l2_name == null || objects[i].l2_name == ""){
									add_text = objects[i].l1_name;
								}else{
									add_text = objects[i].l1_name+', '+objects[i].l2_name;
								}
								var addition = Element('small',{'html':	add_text}).inject(opt, 'bottom');
							}
						},
					}).send();
				}
			}
		},
		'focus' : function(event){
			var pos = this.getCoordinates();
			suggest = $('suggest');
			suggest.setStyle('display', 'none');
			suggest.setStyle('width', pos.width);
			suggest.setStyle('top', pos.top+pos.height);
			suggest.setStyle('left', pos.left);
		},
	})
};
placeEventSetter = function(item){
	item.addEvents({
		'keyup': function(event){
			var iKeyCode = event.keyCode;
			if (!(iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123))) {
				input = item;
				value = input.value;
				parent = input.getParent('tbody');
				place_input = parent.getElements('input[class=place_h]')[0];
				L34_input = parent.getElements('input[class=L34_h]')[0];
				L34_name_input = parent.getElements('input[class=place_L34Object]')[0];
				L0_select = parent.getElements('select[id$=L0Object]')[0];
				type_select = parent.getElements('select[id$=place_type]')[0];
				street_input = parent.getElements('input=[id$=street]')[0];
				house_num_input = parent.getElements('input=[id$=house_num]')[0];
				apartament_num_input = parent.getElements('input=[id$=apartament_num]')[0];
				if(value.length>3){
					req = new Request({
						method: 'get',
						url: '/ajax/place/?obj_name='+value,
						onSuccess: function(response){
							suggest = $('suggest');
							suggest.setStyle('display', 'block');
							suggest.getElements('li').each(function(el){el.destroy()});
							var objects = JSON.decode(response);
							for (var i in objects){
								var opt = Element(
									'li', {
										'class': 'suggest_p',
										'html': objects[i].name+'<br>',
									}).store(
										'obj', objects[i]
									).addEvent('click', function(event){
										obj = this.retrieve('obj');
										input.set('value', obj.name);
										place_input.set('value', obj.id);
										L34_input.set('value', obj.l34+';'+obj.l2+';'+obj.l1);
										L34_name_input.set('value', obj.l34_name);
										street_input.set('value', obj.street);
										house_num_input.set('value', obj.house_num);
										apartament_num_input.set('value', obj.apartament_num);
										L0_select.getElements('option[value='+obj.l0+']')[0].set('selected', 'selected');
										type_select.getElements('option[value='+obj.type+']')[0].set('selected', 'selected');
										suggest.setStyle('display', 'none');
									}).inject(suggest, 'top');
								if(objects[i].l2_name == null){
									add_text = objects[i].l1_name;
								}else{
									add_text = objects[i].l1_name+', '+objects[i].l2_name;
								}
								var addition = Element('small',{'html':	add_text}).inject(opt, 'bottom');
							}
						}							
					}).send();
				}
			}
		},
		'focus' : function(event){
			var pos = this.getCoordinates();
			suggest = $('suggest');
			suggest.setStyle('display', 'none');
			suggest.setStyle('width', pos.width);
			suggest.setStyle('top', pos.top+pos.height);
			suggest.setStyle('left', pos.left);
		},
	})
}
window.addEvent('domready', function(){
	$('suggest').setStyle('position', 'absolute');
	$$('input.place_name').each(placeEventSetter)
	$$('input.place_L34Object').each(L34EventSetter)
	$('add_place_form').addEvent('click', function(event){
		var places = $$('.place')
		var new_form = places[0].clone(true, true);
		var new_id = $('id_form-TOTAL_FORMS').value;
		var el = new_form.getElements('input, select, label, option');
		el.each(function(i){
			tag = i.get('tag');
			if(tag == 'label'){
				for_src = i.get('for');
				for_arr = for_src.split('-');
				for_arr[1] = new_id;
				fo = for_arr.join('-')
				i.set('for', fo);
			}else{
				if(tag == 'input' || tag == 'select'){
					id_src = i.id
					id_arr = id_src.split('-');
					id_arr[1] = new_id;
					id = id_arr.join('-')
					i.set('name', id)
					i.set('id', id);
					i.set('value', '');
				}else{
					if(tag=='option'){
						if(i.get('value')=='---------'){
							i.set('selected', 'selected');
						}
					}
				}
			}
		})
		$('id_form-TOTAL_FORMS').set('value',parseInt($('id_form-TOTAL_FORMS').value)+1);
		new_form.inject('add_place_form', 'before')
		placeEventSetter(new_form.getElements('input[class=place_name]')[0])
		L34EventSetter(new_form.getElements('input[class=place_L34Object]')[0])
	})
})